﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using System.Text;

namespace WebSite.MasterPage
{
    public partial class Setting : System.Web.UI.MasterPage
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Users uinfo = new Users();
                uinfo = UserBase.GetCurrentUser();
                if (uinfo == null)
                {
                    //当前URL地址
                    string url = Request.Url.ToString();
                    Response.Redirect("../Login.aspx?returnurl="+url);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindFunctionItem();//加载菜单
            }
        }

        #region 加载左侧菜单
        private void BindFunctionItem()
        {
            string strfunction = "";

            if (System.Web.HttpContext.Current.Cache.Get("FunctionItemFront") != null)
            {
                strfunction = (String)System.Web.HttpContext.Current.Cache.Get("FunctionItemFront");
                lblFunctionItem.Text = strfunction;
            }
            else
            {
                StringBuilder sbContent = new StringBuilder();
                sbContent.Append("<div class=\"line\"></div>");

                Users uinfo = UserBase.GetCurrentUser();
                string roleid = uinfo.RoleID;
                FunctionsFrontHelper ffhelper = new FunctionsFrontHelper();
                DataTable dtfunction = ffhelper.QueryFunctionsFrontByAllToTable(new Order[] { new Order("Orders", OrderMode.Asc) }, 0, 0, null);
                if (dtfunction != null && dtfunction.Rows.Count > 0)
                {
                    //该权限子菜单
                    PowerItemFrontHelper pfhelper = new PowerItemFrontHelper();
                    Criteria cpower = new Criteria();
                    //cpower.Add(CriteriaType.Equals, "RoleID", uinfo.RoleID);

                    Criteria c1 = new Criteria();
                    c1.Mode = CriteriaMode.Or;
                    c1.Add(CriteriaType.Equals, "RoleID", 0);
                    c1.Add(CriteriaType.Equals, "RoleID", uinfo.RoleID);
                    cpower.Criterias.Add(c1);

                    List<PowerItemFront> listpower = pfhelper.QueryPowerItemFrontBySearch(cpower, null);
                    Criteria crole = new Criteria();
                    for (int i = 0; i < listpower.Count; i++)
                    {
                        crole.Mode = CriteriaMode.Or;
                        crole.Add(CriteriaType.Equals, "ID", listpower[i].ItemID);
                    }

                    foreach (DataRow drfunction in dtfunction.Rows)
                    {
                        string functionid = drfunction["ID"] + "";
                        string functionname = drfunction["FunctionName"] + "";
                        string linkurl = drfunction["LinkUrl"] + "";

                        sbContent.Append("<dl>");
                        sbContent.Append("<dt ><a href=\"#\">" + functionname + "</a></dt>");
                        //sbContent.Append("<dt >" + functionname + "</dt>");


                        //加载该菜单下 该用户权限可以查看的子菜单
                        FunctionItemFrontHelper itemhelper = new FunctionItemFrontHelper();
                        Criteria citem = new Criteria();
                        citem.Add(CriteriaType.Equals, "FunctionID", functionid);
                        citem.Criterias.Add(crole);


                        DataTable dtitem = itemhelper.QueryFunctionItemFrontBySearchToTable(citem, new Order[] { new Order("Orders", OrderMode.Asc) }, 0, 0, null);

                        if (dtitem != null && dtitem.Rows.Count > 0)
                        {
                            foreach (DataRow dritem in dtitem.Rows)
                            {
                                string itemid = dritem["ID"] + "";
                                string itemname = dritem["ItemName"] + "";
                                string pageurl = dritem["PageUrl"] + "";
                                sbContent.Append("<dd ><a href=\"" + pageurl + "\">" + itemname + "</a></dd>");
                            }
                        }

                        sbContent.Append("</dl>");
                    }
                }

                //将菜单放入缓存
                System.Web.HttpContext.Current.Cache.Insert("FunctionItemFront", sbContent.ToString(), null, DateTime.Now.AddHours(12), TimeSpan.Zero);

                lblFunctionItem.Text = sbContent.ToString();
            }
        }
        #endregion
    }
}